$(document).ready(function(){
	$('#category_form').submit(function(){
		return false;
	});
	$('#btn_add_category').click(function(){
		addCategory();
	});
	$('#btn_update_category').click(function(){
		updateCategory();
	});
	$('#btn_delete_category').click(function(){
		deleteCategory();
	});
	$('#btn_clear').click(function(){
		$('#category_vi_name').val('');
		$('#category_en_name').val('');
		$('#category_description').val('');
		$('#category_hidden_id').val(0);
		$('#category_id').val('');
		$('#submit_error').html('');
	});
	$('#btn_search_category').click(function(){
		getCategory();
	});
	$('#btn_search_category_name').click(function(){
		searchCategory();
	});
});

function searchCategory(){
	$('#submit_error').html('');
	var data = {
		keyword: $('#search_category_name').val()
	};
	var url = '';
	if(subview){
		url = base_url + searchSubview(subview);
	}
	$('#search_result').html('');
	var request = {
		url: url,
		type: 'POST',
		data: data,
		success: function(data){
			if(data){
				$('#search_result').html(data);
				$('#category_table').dataTable({
					"bPaginate": true,
					"bLengthChange": false,
					"bFilter": false,
					"bSort": true,
					"bInfo": true,
					"bAutoWidth": false
				});
			} else {
				$('#submit_error').html(ERROR_ID_NOT_FOUND);
			}
		},
		error: function(error){
			$('#submit_error').html(ERROR_ID_NOT_FOUND);
		}
	};
	excuteAjax(request);
}

function getCategory(){
	var category_id = parseInt($('#category_id').val());
	if(category_id){
		$('#submit_error').html('');
		var data = {
			id: category_id
		};
		var url = '';
		if(subview){
			url = base_url + getSubview(subview);
		}
		var request = {
			url: url,
			type: 'POST',
			data: data,
			success: function(data){
				if(data){
					data = JSON.parse(data);
					$('#category_vi_name').val(data.vi_name);
					$('#category_en_name').val(data.en_name);
					$('#category_description').val(data.description);
					$('#category_hidden_id').val(data.id);
				} else {
					$('#submit_error').html(ERROR_ID_NOT_FOUND);
				}
			},
			error: function(error){
				$('#submit_error').html(ERROR_ID_NOT_FOUND);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html(ERROR_INVALID_ID);
	}
}

function addCategory(){
	var category_vi_name = $('#category_vi_name').val();
	var category_en_name = $('#category_en_name').val();
	var category_description = $('#category_description').val();
	$('#submit_error').html('');
	if(category_vi_name !== ''){
		var data = {
			vi_name: category_vi_name,
			en_name: category_en_name,
			description: category_description
		};
		var url = '';
		if(subview){
			url = base_url + addSubview(subview);
		}
		var request = {
			url: url,
			type: 'POST',
			data: data,
			success: function(data){
				if(data == '1'){
					$('#submit_error').html(RESPONSE_SUCCESS);
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html(ERROR_INPUT);
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}

function updateCategory(){
	var category_id = $('#category_hidden_id').val();
	var category_vi_name = $('#category_vi_name').val();
	var category_en_name = $('#category_en_name').val();
	var category_description = $('#category_description').val();
	$('#submit_error').html('');
	if(category_vi_name !== '' && category_id != 0){
		var data = {
			id: category_id,
			vi_name: category_vi_name,
			en_name: category_en_name,
			description: category_description
		};
		var url = '';
		if(subview){
			url = base_url + updateSubview(subview);
		}
		var request = {
			url: url,
			type: 'POST',
			data: data,
			success: function(data){
				if(data == '1'){
					$('#submit_error').html(RESPONSE_SUCCESS);
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html(ERROR_INPUT);
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}

function deleteCategory(){
	var category_id = $('#category_hidden_id').val();
	$('#submit_error').html('');
	if(category_id != 0){
		var data = {
			id: category_id
		};
		var url = '';
		if(subview){
			url = base_url + deleteSubview(subview);
		}
		var request = {
			url: url,
			type: 'POST',
			data: data,
			success: function(data){
				if(data == '1'){
					$('#submit_error').html(RESPONSE_SUCCESS);
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html(ERROR_INPUT);
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}